home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: printing pid_t...
- Date: Tue, 20 Feb 96 23:34:04 GMT
- Organization: none
- Message-ID: <824859244snz@genesis.demon.co.uk>
- References: <4gcv6f$6jf@cisunix1.dfci.harvard.edu>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <4gcv6f$6jf@cisunix1.dfci.harvard.edu>
- gotd@jimmy.harvard.edu "Godfrey Degamo" writes:
-
- >Hello. Sorry to bother you with this question. I don't know
- >if this belongs in comp.unix.programmers or here.
- >
- >Anyways, I want to read and write pid_t to a text file. The problem
- >is how is pid_t type defined? As an int, long, unsigned int or
- >unsigned long. (I think that's all the types that pid_t can be...)
- >Can I be safe to assume unsigned long and use %lu for printing and
- >scanning?
-
- A major reason for creating a type alias such as pid_t is to allow the
- implementation to choose the most appropriate type for it. While pid_t
- itself is Unix/POSIX specific the problem of printing values such as this
- is a more general one. For example the C language defines a type size_t
- which is the type of the result of the sizeof operator. size_t is defined
- by the standard to be an 'unsigned integral type' but you don't know which.
- However you can cast any unsigned integral type to unsigned long while
- preserving the value so the portable way of printing the size of a variable
- x is:
-
- printf("%lu\n", (unsigned long) sizeof x);
-
- Similarly the C language standard defines ptrdiff_t to be "the signed
- integral type of the result of subtracting two pointers" so you could
- portably write:
-
- printf("%ld\n", (long)(ptr2-ptr1));
-
- I don't know if pid_t is defined to be a signed type or not but its usage
- in fork() and especially kill() suggests that it won't in practice hold
- a value which can't be represented by a long, so I'd use a form equivalent to
- the 2nd example. Direct further discussion of pid_t to comp.unix.programmer.
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-